package com.ly.cloud.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.google.common.base.Strings;
import com.ly.cloud.dto.TeacherDto;
import com.ly.cloud.entity.TeacherPo;
import com.ly.cloud.exception.CloudException;
import com.ly.cloud.exception.biz.BusinessException;
import com.ly.cloud.exception.mapper.DBException;
import com.ly.cloud.mapper.TeacherMapper;
import com.ly.cloud.service.TeacherService;
import com.ly.cloud.vo.TeacherDetailVo;
import com.ly.cloud.vo.TeacherDmjVo;
import com.ly.cloud.vo.TeacherSimpleVo;
import com.ly.cloud.vo.TeacherVo;

/**
 * 师资管理Impl
 * @author caihuihai
 * @date 2018年12月13日 下午3:24:36
 */
@Service
@Transactional(readOnly = true, propagation = Propagation.NOT_SUPPORTED)
public class TeacherServiceImpl implements TeacherService {
	
	@Autowired
	private TeacherMapper teacherMapper;

	/**
	 * 分页查询教职工
	 */
	@Override
	public Page<TeacherVo> selectPage(Page<TeacherVo> page,TeacherDto dto) {
		try {
			return page.setRecords(teacherMapper.selectPage(page,dto));
		} catch (Exception e) {
			throw new CloudException("查询异常，请重试!", e);
		}
	}

	/**
	 * 新增或者修改教职工
	 */
	@Override
	public boolean submitJzg(TeacherDto dto, String loginUserName) {
		try {
			int tag = 0;
			TeacherPo po = new TeacherPo();
			BeanUtils.copyProperties(dto, po);
			if(Strings.isNullOrEmpty(dto.getType())) {  //操作类型为空
				throw new BusinessException();
			}
			if("add".equals(dto.getType())) {  //新增
				po.setCjsj(new Date());
				po.setCjr(loginUserName);
				tag = teacherMapper.insert(po);
			}else {  //修改
				po.setXgsj(new Date());
				po.setXgr(loginUserName);
				EntityWrapper<TeacherPo> ew = new EntityWrapper<>();
				ew.where("gh={0}", po.getGh());
				tag = teacherMapper.update(po, ew);
			}
			return tag>0?true:false;
		} catch (BusinessException e) {
			throw new BusinessException("类型type必填", e);
		} catch (Exception e) {
			throw new CloudException("操作异常，请重试!", e);
		}
	}

	/**
	 * 根据工号删除教职工信息
	 */
	@Override
	public boolean deleteByGh(String gh) {
		try {
			EntityWrapper<TeacherPo> ew = new EntityWrapper<>();
			ew.where("gh={0}", gh);
			return teacherMapper.delete(ew)>0?true:false;
		} catch (Exception e) {
			throw new DBException("删除教职工失败！", e);
		}
	}

	/**
	 * 批量删除教职工信息
	 */
	@Override
	public boolean deleteByGhs(List<String> ghs) {
		try {
			for (String gh : ghs) {
				EntityWrapper<TeacherPo> ew = new EntityWrapper<>();
				ew.where("gh={0}", gh);
				teacherMapper.delete(ew);
			}
			return true;
		} catch (Exception e) {
			throw new DBException("批量删除教职工失败！", e);
		}
	}

	/**
	 * 根据工号查询教职工信息
	 */
	@Override
	public TeacherDetailVo findTeacherByGh(String gh) {
		try {
			TeacherDetailVo vo = new TeacherDetailVo();
			TeacherPo po = new TeacherPo();
			po.setGh(gh);
			TeacherPo selPo = teacherMapper.selectOne(po);
			BeanUtils.copyProperties(selPo, vo);
			return vo;
		} catch (Exception e) {
			throw new DBException("查询教职工失败！", e);
		}
	}

	/**
	 * 查询教职工列表（下拉）
	 */
	@Override
	public List<TeacherSimpleVo> findTeachList() {
		try {
            return teacherMapper.findTeachList();
        } catch (Exception e){
            e.printStackTrace();
            throw new CloudException("查询失败!", e);
        }
	}

	@Override
	public List<TeacherDmjVo> queryDmjList() {
		try {
			List<TeacherDmjVo> list = new ArrayList<TeacherDmjVo>();
			String[] dms = new String[] {"XTGL_SFZLXM","XTGL_GJDQDM","XTGL_MZM","XTGL_ZZMMM","XTGL_ZCDM","XTGL_BZLBM","XTGL_JZGLBDM","XTGL_JZGDQZTDM"};
			String[] lxs = new String[] {"sfzh","gjdq","mzm","zzmm","zwdm","bzlb","jzglx","dqzt"};  //sfzh身份证类型，gjdq国籍或地区，mzm民族，zzmm政治面貌，zwdm职务级别，bzlb编制类别，jzglx教职工类别，dqzt当前状态
			for(int i=0; i<dms.length; i++) {
				TeacherDmjVo vo = new TeacherDmjVo();
				vo.setLx(lxs[i]);
				vo.setList(teacherMapper.findDmjList(dms[i]));
				list.add(vo);
			}
			return list;
		} catch (Exception e) {
			e.printStackTrace();
			throw new CloudException("查询失败!", e);
		}
	}

}
